package easy;

import util.PrintUtils;

public class Solution_506 {

    /*public String[] findRelativeRanks(int[] score) {
        int[] index = new int[score.length];
        System.arraycopy(score, 0, index, 0, score.length);
        Arrays.sort(index);
        Map<Integer, Integer> map = new HashMap<>(10000);

        for (int i = 0; i < index.length; i++) {
            map.put(index[i], index.length - i);
        }

        String[] res = new String[score.length];
        for (int i = 0; i < score.length; i++) {
            int sort = map.get(score[i]);
            String val = null;
            if (sort > 3) {
                val = String.valueOf(sort);
            } else if (sort == 1) {
                val = "Gold Medal";
            } else if (sort == 2) {
                val = "Silver Medal";
            } else if (sort == 3) {
                val = "Bronze Medal";
            }
            res[i] = val;
        }

        return res;
    }*/

    public String[] findRelativeRanks(int[] score) {
        int max = score[0];
        for (int i = 0; i < score.length; i++) {
            max = Math.max(max, score[i]);
        }

        int[] index = new int[max + 1];
        for (int i = 0; i < score.length; i++) {
            index[score[i]] = i + 1;
        }

        for (int i = max, sort = 1; i >= 0; i--) {
            if (index[i] != 0) {
                index[i] = sort++;
            }
        }

        String[] res = new String[score.length];
        for (int i = 0; i < score.length; i++) {
            int sort = index[score[i]];
            String val = null;
            if (sort > 3) {
                val = String.valueOf(sort);
            } else if (sort == 1) {
                val = "Gold Medal";
            } else if (sort == 2) {
                val = "Silver Medal";
            } else if (sort == 3) {
                val = "Bronze Medal";
            }
            res[i] = val;
        }

        return res;
    }

    public static void main(String[] args) {
        Solution_506 model = new Solution_506();
        String[] array = model.findRelativeRanks(new int[]{5, 4, 3, 2, 1});
//        String[] array = model.findRelativeRanks(new int[]{10,3,8,9,4});
//        String[] array = model.findRelativeRanks(new int[]{100,111,111111});
        PrintUtils.array(array);
    }
}
